package domain.player;

import domain.player.ai.*;
import domain.GameGrid;
import java.awt.Color;

/**
 * Computer represents a Computer player
 */
public class Computer extends Player {
	/** Used for getting a good move. */
	private AI ai;
	private int aiType;

	/** Constructor.
	 * @param name
	 * @param discID
	 * @param discColor
	 */
    public Computer(String name, Disc discID, Color discColor) {
		super(name, discID, discColor);
		
		// setAIType(AI.AI_MINMAX);
                // setAIType(AI.AI_MINIMAX);
		//setAIType(AI.AI_HASHMAP);

		setAIType(AI.AI_MINIMAX, false, true, 7);
    }

	public Computer(String name, Disc discID, Color discColor, boolean debug, boolean alphaBeta, int ply){
		super(name, discID, discColor);

		setAIType(AI.AI_MINIMAX, debug, alphaBeta, ply);
	}

	public void setAIType(int aiConstant, boolean debug, boolean alphaBeta, int ply){
	    if(aiConstant == AI.AI_MINIMAX){
			//ai = new AI_Minimax( getDiscID(), debug, alphaBeta, ply);
			ai = new AI_Minimax(getDiscID(), debug, alphaBeta, ply);
			aiType = AI.AI_MINIMAX;
		} else if(aiConstant == AI.AI_MINMAX){
                        ai = new AI_MMAB(getDiscID(), debug, alphaBeta, ply);
			aiType = AI.AI_MINMAX;
                } else {
			ai = new AI_HashMap( getDiscID() );
			aiType = AI.AI_HASHMAP;
	    }

	}
        
        public void setAIType(int aiConstant){
	    if(aiConstant == AI.AI_MINMAX && aiType != AI.AI_MINMAX) {
			ai = new AI_MMAB( getDiscID() );
                        // ai = new AI_Minimax(getDiscID());
			aiType = AI.AI_MINMAX;
		} else if(aiConstant == AI.AI_MINIMAX && aiType  != AI.AI_MINIMAX){
                    ai = new AI_Minimax(getDiscID());
                    aiType = AI.AI_MINIMAX;
                } else{
			ai = new AI_HashMap( getDiscID() );
			aiType = AI.AI_HASHMAP;
	    }
		
	}

	public int getAIType(){
		return aiType;
	}

	public void setPly(int depth){
		if(aiType == AI.AI_MINIMAX)
			((AI_Minimax)ai).setPly(depth);
	}

	public void setDebug(boolean debug){
		if(aiType == AI.AI_MINIMAX)
			((AI_Minimax)ai).setDebug(debug);
	}

	public int getMovesAhead(){
		if(aiType == AI.AI_MINIMAX){
			return ((AI_Minimax)ai).getPly();
                } else if(aiType == AI.AI_MINMAX){
                    return ((AI_MMAB)ai).getPly();
                } else{
			return AI_Minimax.DEFAULT_DEPTH;
                }
	}

	@Override
	public int getNextMove(GameGrid gameGrid) {
		int move = ai.getGoodMove(gameGrid);

		try {
			Thread.sleep(100);
		} catch (InterruptedException ex) {
			System.out.println("InterruptedException: " + ex);
		}

		return move;
	}

        public int getNrOfEvaluatedMoves(){
            if(ai instanceof AI_Minimax){
                return ((AI_Minimax)ai).getNrOfTriedMoves();
            } else{
                return ((AI_MMAB)ai).getNrOfTriedMoves();
            }
        }

        public int getScoreForMove(){
            if(ai instanceof AI_Minimax){
                return ((AI_Minimax)ai).getMoveScore();
            } else{
                return ((AI_MMAB)ai).getMoveScore();
            }
            
        }

	@Override
	public PlayerType getType() {
		return PlayerType.COMPUTER;
	}

	@Override
	public String toString() {
		return super.toString()
				+ ", type = computer, color = " + getDiscColor();
	}
}
